//
//  snake.m
//  Snake
//
//  Created by 李毅 on 16/9/18.
//  Copyright © 2016年 李毅. All rights reserved.
//

#import "snake.h"
#import "Node.h"
@implementation snake

-(id) initWithXY:(int)x:(int)y{
    
    self = [snake new];
    self.body = [NSMutableArray array];
    [self.body addObject:[[Node alloc] initWithXY:0 :0] ];
    [self.body addObject:[[Node alloc] initWithXY:0 :1]];
    [self.body addObject:[[Node alloc] initWithXY:0 :2]];
    [self.body addObject:[[Node alloc] initWithXY:0 :3]];
    [self.body addObject:[[Node alloc] initWithXY:0 :4]];
    
    self.dirct = 4;
    self.pre_direct = 4;
    self.header = [self.body lastObject];
    return self;
    
}


-(void) grow{
    if (_tail) {
        [self.body insertObject:_tail atIndex:0];
    }
    
    
    
}

-(void) destory{
    
}

-(void) changeD:(int) direct{
    
    self.dirct = direct;
}

-(void)start{
    float vT = 0.2 - ([self.body count]*1.0f/10.0f)*0.2f;
    if (vT < 0.03) {
        vT = 0.03f;
        NSLog(@"run at max speed...");
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:vT target:self selector:@selector(move) userInfo:nil repeats:YES];
    self.live = YES;
    
}

-(void) move{
//    if (((self.pre_direct + self.dirct == 4 )||(self.pre_direct + self.dirct == 6)) &&(self.pre_direct != self.dirct)) {
//        //invalid
//        return;
//    }
//    
    
    switch (self.dirct) {
        case 1:{
            _tail = [_body objectAtIndex:0];
            [self.body removeObjectAtIndex:0];
            Node *lastone = [self.body lastObject];
            Node *newnode = [[Node alloc] initWithXY:lastone.x-1:lastone.y];
            [self.body insertObject:newnode atIndex:[self.body count] ];
            self.header = newnode;
            break;
        }
        case 2:{
            _tail = [_body objectAtIndex:0];

            [self.body removeObjectAtIndex:0];
            Node *lastone = [self.body lastObject];
            Node *newnode = [[Node alloc] initWithXY:lastone.x:lastone.y-1];
            [self.body insertObject:newnode atIndex:[self.body count] ];
            self.header = newnode;

            break;
        }
        case 3:{
            _tail = [_body objectAtIndex:0];

            [self.body removeObjectAtIndex:0];
            Node *lastone = [self.body lastObject];
            Node *newnode = [[Node alloc] initWithXY:lastone.x+1:lastone.y];
            [self.body insertObject:newnode atIndex:[self.body count] ];
            self.header = newnode;

            break;
        }
        case 4:{
            _tail = [_body objectAtIndex:0];

            [self.body removeObjectAtIndex:0];
            Node *lastone = [self.body lastObject];
            Node *newnode = [[Node alloc] initWithXY:lastone.x:lastone.y+1];
            [self.body insertObject:newnode atIndex:[self.body count] ];
            self.header = newnode;

            break;
        }
        default:
            break;
    }
    
    if (_moveFinishBlock) {
        _moveFinishBlock();
    }
    
    
}

-(void) reset{
    self.body = [NSMutableArray array];
    [self.body addObject:[[Node alloc] initWithXY:0 :0] ];
    [self.body addObject:[[Node alloc] initWithXY:0 :1]];
    [self.body addObject:[[Node alloc] initWithXY:0 :2]];
    [self.body addObject:[[Node alloc] initWithXY:0 :3]];
    [self.body addObject:[[Node alloc] initWithXY:0 :4]];

    self.dirct = 4;
    self.pre_direct = 4;
    self.header = [self.body lastObject];
    
    [self.timer invalidate];
    self.timer = nil;
    
    self.live = NO;

    

    
}

@end
